package com.rfsp.common.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.rfsp.common.interceptor.AjaxInterceptor;
import com.rfsp.common.interceptor.LoggerInterceptor;
import com.rfsp.common.interceptor.SessionInterceptor;
import com.rfsp.common.interceptor.TokenInterceptor;

/**
 * @author zhangboqing
 * @date 2019-07-28
 *
 * MVC配置类
 */
@Configuration
public class MvcConfigurer implements WebMvcConfigurer {

	@Value("${mcp.img.path")
	String filePath;

	@Autowired
	AjaxInterceptor ajaxInterceptor;
	@Autowired
	LoggerInterceptor loggerInterceptor;
	@Autowired
	SessionInterceptor sessionInterceptor;
	@Autowired
	TokenInterceptor tokenInterceptor;

	/**
	 * 拦截器配置
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {

		String[] includePatterns = {"/**"};
		String[] excludePatterns = {
				"/**/*.html", "/**/*.js", "/**/*.css", "/**/*.map", "/**/*.json", "/**/*.pdf",//静态文件
				"/**/*.png", "/**/*.gif", "/**/*.svg", "/**/*.ico", "/**/*.mp4", //图片文件
				"/**/*.eot", "/**/*.ttf", "/**/*.woff", "/**/*.woff2"};//controller
		//String[] controllerPatterns = {"/demo/*","/common/*", "/test/*", "/base/**", "/hos/**", "/bank/**", "/uploader2/upload","/error"};//controller
		String[] controllerPatterns = {"/captcha","/demo/*","/common/*", "/test/*","/error","/error/*","/bank/businessRules1/*","/interfaceclient/orgDirectory/queryOrgDirectory"};//controller
		String[] devPatterns = {"/base/**", "/hos/**", "/bank/**","/demo/**","/uploader2/upload"};//dev

		registry.addInterceptor(loggerInterceptor)
				.addPathPatterns(includePatterns)
				.excludePathPatterns(excludePatterns);

		registry.addInterceptor(tokenInterceptor)
				.addPathPatterns(includePatterns)
				.excludePathPatterns(excludePatterns);

		registry.addInterceptor(sessionInterceptor)
				.addPathPatterns(includePatterns)
				.excludePathPatterns(excludePatterns)
				.excludePathPatterns(controllerPatterns)
				//.excludePathPatterns(devPatterns)
				.excludePathPatterns("/", "/mcp/", "/login/*");
//    	
//    	registry.addInterceptor(ajaxInterceptor)
//    	.addPathPatterns(includePatterns)
//    	.excludePathPatterns(excludePatterns);
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {

		//注意如果filePath是写死在这里，一定不要忘记尾部的/或者\\，这样才能读取其目录下的文件
		registry.addResourceHandler("/mcp/img/**").addResourceLocations("file:"+filePath);

	}

	/**
	 * 拦截器配置
	 */
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {

		registry.addViewController("/").setViewName("index");
		registry.addViewController("/index.html").setViewName("index");
		registry.addViewController("/main.html").setViewName("main");

	}


//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        converters.add(marshallingHttpMessageConverter());
//    }
//
//    public MarshallingHttpMessageConverter marshallingHttpMessageConverter(){
//        MarshallingHttpMessageConverter marshallingHttpMessageConverter = new MarshallingHttpMessageConverter();
//        List<MediaType> mediaTypes = new ArrayList<MediaType>();
//        mediaTypes.add(MediaType.TEXT_XML);
//        mediaTypes.add(MediaType.APPLICATION_XML);
//        XStreamMarshaller xStreamMarshaller=new XStreamMarshaller();
//        marshallingHttpMessageConverter.setSupportedMediaTypes(mediaTypes);
//        marshallingHttpMessageConverter.setMarshaller(xStreamMarshaller);
//        marshallingHttpMessageConverter.setUnmarshaller(xStreamMarshaller);
//        return marshallingHttpMessageConverter;
//    }

	//配置文件上传
	@Bean(name = {"multipartResolver"})
	public MultipartResolver multipartResolver(){
		CommonsMultipartResolver commonsMultipartResolver=new CommonsMultipartResolver();
		commonsMultipartResolver.setDefaultEncoding("utf-8");
		commonsMultipartResolver.setMaxUploadSize(10485760000L);
		commonsMultipartResolver.setMaxInMemorySize(40960);
		return commonsMultipartResolver;
	}

}

